
var downfileurl = sthost + "downloadFile/";
var userid = localStorage.getItem("userid");
var usercard = localStorage.getItem("usercard");


function getBuns() {
    $.ajax({
        type: "get",
        url: sthost + "selectBunsAll",
        async: true,
        success: function (obj) {
            //console.log(JSON.stringify(data));
            malls.items = setData(obj.data);

        }
    });
}


var currentClick = false;
var index3;
mui.init()

mui.ready(function () {
    localStorage.setItem("userid", userid);
    $('#span-buns-order').click(function () {
        window.location = "bunsorderlist.html";
    });
    listenBack();
    getBuns();
    mui('body').on('tap', '.mui-popup-backdrop', function () {
        mui.closePopup();
    });
    $('.div-shop-car-content').toggle();
    $('.div_bottom_parent p').click(function () {
        if (malls.totalPrice > 0 && $('.div-shop-car').css('display') == 'none') {
            $('.div-shop-car').css('display', 'block');
            $('.div-shop-car-content').slideDown(400, function () {
            });
        } else {
            $('.div-shop-car-content').slideUp(400, function () {
                $('.div-shop-car').css('display', 'none');
            });
        }
    });

    $('.div-shop-car').click(function () {
        $('.div-shop-car-content').slideUp(400, function () {
            $('.div-shop-car').css('display', 'none');
        });
    });

    $('.div-shop-car-content').click(function (e) {
        e.stopPropagation();
    });

    $('#p-clear').click(function () {
        /*清空购物车*/
        mui.confirm("确定清空购物车吗？", "提示", ['取消', '确认'], function (e) {
            if (e.index == 0) {
            } else {
                $('.div-shop-car-content').slideUp(400, function () {
                    $('.div-shop-car').css('display', 'none');
                    malls.totalPrice = 0;
                    malls.totalNum = 0;
                    for (var a = 0; a < malls.items.length; a++) {
                        var mall1 = malls.items[a];
                        if (mall1.chanceSortNum > 0) {
                            mall1.chanceSortNum = 0;
                        }
                        for (var i = 0; i < mall1.infos.length; i++) {
                            var info1 = mall1.infos[i];
                            if (info1.chancenum > 0) {
                                info1.chancenum = 0;
                            }
                        }
                    }
                });
            }
        }, 'div')

    });

    //malls.items = setData();
    $(".right").on('scroll', function () {
        if (currentClick) {
            currentClick = false;
            return;
        }
        var top = $('.right').scrollTop();
        var items1 = $('.right_list_title').find('.right_list_item');
        for (var index = 0; index < items1.length; index++) {
            var itemTop = items1.eq(index).offset().top;
            var height1 = items1.eq(index).height();
            var leftitem = $('.left').find('a').eq(index).height();
            var left = $('.left').find('a').eq(index).attr('class');
            var lefttop = $('.left').find('a').eq(index).offset().top;
            if (itemTop >= -height1 && itemTop <= height1) {
                if (leftitem * (index + 1) > ($('.left').scrollTop() + $('.left').height())) {
                    //console.log("lefttop+"+lefttop+"index="+index);
                    var leftbottom = (index + 1) * leftitem - $('.left').height();
                    $('.left').scrollTop(leftbottom);
                }

                if (left === 'press_a') {
                    var leftparenttop = $('.left').scrollTop();
                    console.log(leftparenttop + "leftparenttop" + "index=" + index);
                    if (index == 0) {
                        $('.left').scrollTop(0);
                    } else {
                        if (Math.abs(lefttop) <= leftparenttop && lefttop <= 0) {
                            var offy = leftparenttop - Math.abs(lefttop);
                            console.log("offy" + offy);
                            $('.left').scrollTop(offy);
                        }
                    }
                }
                malls.index1 = index;
                break;
            }
        }
    });
});

//商品列表
var malls = new Vue({
    el: '#malls',
    data: {
        cars: {
            userid: userid,
            totalPrice: 0,
            infos: []
        },
        items: [], //商品列表信息流数据
        index1: 0, //选中
        totalPrice: 0, //总价
        totalNum: 0 //总数量
    },
    methods: {
        gobuy: function () {
            malls.cars.infos = [];
            if (malls.totalPrice > 0) {
                //alert("dasda");
                malls.cars.totalPrice = malls.totalPrice;
                malls.cars.userid = userid;
                for (var i in malls.items[0].infos) {
                    var item = malls.items[0].infos[i];
                    if (item.chancenum > 0) {
                        malls.cars.infos.push(item);
                    }
                }
                sessionStorage.setItem("cars", JSON.stringify(malls.cars));
                window.location = "confirmorder.html";
            }

        },
        aonlick: function (index, obj) {
            currentClick = true;
            this.index1 = index;
            var el = event.currentTarget;
            location.replace(el.href);
            /*var a = $('.left').find('a').eq(index);
            var href = a.href;
            location.replace(href);*/
            console.log("index" + index)
        },
        jiaonclick: function (sortindex, infoindex, event) {
            /*items[sortindex].*/
            if (malls.items[sortindex].infos[infoindex].chancenum >= malls.items[sortindex].infos[infoindex].bunsnum) {
                mui.toast("无法继续添加!");
                return;
            }
            malls.totalNum += 1;
            malls.items[sortindex].chanceSortNum += 1;
            malls.items[sortindex].infos[infoindex].chancenum += 1;
            malls.items[sortindex].infos[infoindex].unittotalprice = accMul(malls.items[sortindex].infos[infoindex].bunsunitprice, malls.items[sortindex].infos[infoindex].chancenum);
            malls.totalPrice = accAdd(malls.totalPrice, malls.items[sortindex].infos[infoindex].bunsunitprice);
            //var el = event.currentTarget;
            //curveMove(el);
            //paramove(el);
        },
        jianonclick: function (sortindex, infoindex) {
            malls.totalNum -= 1;
            malls.items[sortindex].chanceSortNum -= 1;
            malls.items[sortindex].infos[infoindex].chancenum -= 1;
            malls.items[sortindex].infos[infoindex].unittotalprice = accMul(malls.items[sortindex].infos[infoindex].bunsunitprice, malls.items[sortindex].infos[infoindex].chancenum);
            malls.totalPrice = Subtr(malls.totalPrice, malls.items[sortindex].infos[infoindex].bunsunitprice);

            if (malls.totalPrice <= 0) {
                $('.div-shop-car-content').slideUp(400, function () {
                    $('.div-shop-car').css('display', 'none');
                });
            }
        }
    }

});

//制造数据
function setData(data) {
    var newitems = [{
        name: "包子",
        active: true,
        chanceSortNum: 0,
        infos: []
    }];
    for (var i in data) {
        newitems[0].infos.push({
            bunsid: data[i].bunsid,
            chancenum: 0,
            bunsname: data[i].bunsname,
            bunsnum: data[i].bunsnum,
            unittotalprice: 0,
            bunsunitprice: data[i].bunsunitprice,
            filepath: downfileurl + data[i].goodpic.filepath

        });
    }
    return newitems;
}


var curveMove = function (node1) { // node 为点击的节点
    var node = $(node1);
    var
        xStar = node.offset().left - 5, // 获取起始点横坐标
        yStar = node.offset().top - 10, // 获取起始点纵坐标

        nodeOffset = $('#span-bottom-dot-num').offset(), // 购物车偏移量
        xEnd = nodeOffset.left, // 结束点横坐标
        yEnd = nodeOffset.top; // 结束点纵坐标
    var outer = $('<button id="parent-car-div" class="btn-add-reduce" style="position: fixed;z-index: 99;"></button>');
    outer.fly({
        start: {
            left: xStar,
            top: yStar
        },
        end: {
            left: xEnd,//抛物体终点横坐标
            top: yEnd, //抛物体终点纵坐标
            width: 0,
            height: 0
        },
        onEnd: function () {
            //$('#parent-car-div').remove();
        }

    });

};


var paramove = function (node1) {
    // 初始化抛物线动画
    var node = $(node1);
    var
        xStar = node.offset().left - 5, // 获取起始点横坐标
        yStar = node.offset().top - 10, // 获取起始点纵坐标

        nodeOffset = $('#span-bottom-dot-num').offset(), // 购物车偏移量
        xEnd = nodeOffset.left, // 结束点横坐标
        yEnd = nodeOffset.top; // 结束点纵坐标

    var myParabola = funParabola(node, document.querySelector("#span-bottom-dot-num"), {
        speed: 50,// 每帧移动的像素大小
        curvature: 0.005,// 实际指焦点到准线的距离
        curvature: 0.005,// 实际指焦点到准线的距离
        complete: function () {
            document.getElementById('parent-car-div').style.visibility = "hidden";
        }
    });
    $('<button id="parent-car-div" class="btn-add-reduce" style="position: fixed;z-index: 99;"></button>').css({
        left: xStar,
        top: yStar,
        visibility: 'visible'
    });

    myParabola.position().move();
}




function listenBack() {
    window.history.pushState(null, null, location.href);
    window.addEventListener("popstate", function(e) {
        if( $('.div-shop-car').css('display') != 'none'){
            		//alert("进来了");
                    console.log("-------------popstate---");
                    window.history.pushState(null, null, location.href);

                    $('.div-shop-car-content').slideUp(400, function() {
                        $('.div-shop-car').css('display', 'none');
                    });
            }else{
            goHistory();
        }

    })

}


function goHistory() {
    var u = navigator.userAgent,
        app = navigator.appVersion;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if(isAndroid) {
        console.log('android');
        window.app.back();
    } else if(isIOS) {
        console.log('ios');
        window.webkit.messageHandlers.GoBack.postMessage(null);
    }
}
$(".tp-back").on("click", function() {

    goHistory();
});